package com.etime.shyauth.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shyauth.config.BaseMessage;
import com.etime.shyauth.config.ListMessage;
import com.etime.shyauth.config.PageMessage;
import com.etime.shyauth.config.PojoMessage;
import com.etime.shyauth.pojo.SysRole;
import com.etime.shyauth.services.interfaces.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/sys/role")
@ResponseBody
@Api(tags = "系统角色管理数据接口")
public class SysRoleController {
    @Autowired
    private SysRoleService service;

    @ApiOperation("插入系统角色")
    @ApiImplicitParam(name = "role",value = "角色实体，不用传递roleid")
    @PostMapping("/insert")
    public BaseMessage insertSysRole(SysRole role){
        int rtn = service.insertSysRole(role);
        BaseMessage msg = new BaseMessage();
        msg.setCode(rtn);
        return msg;
    }

    @ApiOperation("根据id修改角色信息")
    @ApiImplicitParam(name = "role",value = "必须传递roleid")
    @PostMapping("/update")
    public BaseMessage updateSysRoleById(SysRole role){
        int rtn = service.updateSysRoleById(role);
        BaseMessage baseMessage = new BaseMessage();
        baseMessage.setCode(rtn);
        return baseMessage;
    }

    @ApiOperation("根据id查询一条角色的详细信息")
    @ApiImplicitParam("角色id")
    @GetMapping("/get/{roleid}")
    public PojoMessage<SysRole> selectRoleById(@PathVariable("roleid") int roleid){
        SysRole role = service.selectRoleById(roleid);
        PojoMessage<SysRole> msg = new PojoMessage<>();
        if(role==null){
            msg.setCode(0);
            msg.setMsg("没有指定的数据");
        }else {
            msg.setData(role);
            msg.setCode(1);
        }
        return msg;
    }

    @ApiOperation("查询角色列表")
    @ApiImplicitParam("可接收参数：rolename,rolestate")
    @GetMapping("/list")
    public ListMessage<SysRole> selectRoleList(SysRole role){
        List<SysRole> list = service.selectRoleListByCondition(role);
        ListMessage<SysRole> msg = new ListMessage<>();
        if(list!=null){
            msg.setCode(1);
            msg.setDatalist(list);
        }else{
            msg.setCode(0);
            msg.setDatalist(list);
            msg.setMsg("没有指定的数据");
        }
        return msg;
    }

    @ApiOperation("分页查询角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "role",value = "查询条件，可接收参数：rolename,rolestate"),
            @ApiImplicitParam(name = "pagenum",value = "第几页"),
            @ApiImplicitParam(name = "pagesize",value = "每页多少行")}
    )
    @GetMapping("/page")
    public PageMessage<SysRole> selectRoleByPage(SysRole role, Long pagenum, Long pagesize){
        Page<SysRole> pagedata = service.selectRoleByPage(role,pagenum,pagesize);
        PageMessage<SysRole> msg = new PageMessage<>();
        if(pagedata!=null) {
            msg.setCode(1);
            msg.setPagedata(pagedata);
        }else{
            msg.setCode(0);
            msg.setMsg("没有查询到数据");
        }
        return msg;
    }
}
